package com.dzl.shoppingCart.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.shoppingCart.pojo.WelfareUserShoppingCart;
import com.dzl.shoppingCart.dao.WelfareUserShoppingCartMapper;
import com.dzl.shoppingCart.pojo.query.WelfareUserShoppingCartQuery;
import com.dzl.shoppingCart.pojo.vo.WelfareUserShoppingCartSupplierGoodsVo;
import com.dzl.shoppingCart.pojo.vo.WelfareUserShoppingCartSupplierListVo;
import com.dzl.shoppingCart.pojo.vo.WelfareUserShoppingCartSupplierVo;
import com.dzl.shoppingCart.pojo.vo.WelfareUserShoppingCartVo;
import com.dzl.shoppingCart.service.WelfareUserShoppingCartService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 小程序用户购物车(WelfareUserShoppingCart)表服务实现类
 * @author ChenLong
 * @since 2021-05-20 11:33:09
 */
@Service("welfareUserShoppingCartService")
public class WelfareUserShoppingCartServiceImpl extends BaseServiceImpl<WelfareUserShoppingCart> implements WelfareUserShoppingCartService {

    @Resource
    private WelfareUserShoppingCartMapper welfareUserShoppingCartMapper;

    @Override
    public boolean deleteById(Integer id, Integer userId) {
        return this.welfareUserShoppingCartMapper.deleteById(id, userId) > 0;
    }

    @Override
    public boolean deleteBatchByIdList(List<Integer> idList, Integer userId) {
        return this.welfareUserShoppingCartMapper.deleteBatchByIdList(idList, userId) > 0;
    }

    @Override
    public List<WelfareUserShoppingCartVo> getUserCartGoodsList(WelfareUserShoppingCartQuery query) {
        PageHelper.startPage(query);
        return welfareUserShoppingCartMapper.getUserCartGoodsList(query);
    }

    @Override
    public WelfareUserShoppingCartSupplierVo getUserCartGoodsListGroupBySupplierByIdList(List<Integer> list,String area) {
        WelfareUserShoppingCartSupplierVo welfareUserShoppingCartSupplierVo = new WelfareUserShoppingCartSupplierVo();
        List<WelfareUserShoppingCartSupplierListVo> welfareUserShoppingCartVoList = welfareUserShoppingCartMapper.getUserCartGoodsListGroupBySupplierByIdList(list);
        BigDecimal totalCreditsAmount = BigDecimal.ZERO;
        if (EmptyUtils.isNotEmpty(welfareUserShoppingCartVoList) && welfareUserShoppingCartVoList.size() > 0) {
            for (WelfareUserShoppingCartSupplierListVo welfareUserShoppingCartSupplierListVo : welfareUserShoppingCartVoList) {
                // 校验该地区是否支持发货
                if (EmptyUtils.isNotEmpty(area) && EmptyUtils.isNotEmpty(welfareUserShoppingCartSupplierListVo.getRestrictedAreas())){
                    String[] restrictedAreasArray = welfareUserShoppingCartSupplierListVo.getRestrictedAreas().split(",");
                    if (restrictedAreasArray.length > 0) {
                        for (int i = 0; i < restrictedAreasArray.length; i++) {
                            if (area.equals(restrictedAreasArray[i])){
                                welfareUserShoppingCartSupplierListVo.setIfDelivery(1);
                                welfareUserShoppingCartSupplierVo.setIfDelivery(1);
                                break;
                            }
                        }
                    }
                }
                if (EmptyUtils.isNotEmpty(welfareUserShoppingCartSupplierListVo.getCartGoodsList())) {
                    BigDecimal supplierCreditsAmount = BigDecimal.ZERO;
                    List<WelfareUserShoppingCartSupplierGoodsVo> welfareUserShoppingCartSupplierGoodsVoList = welfareUserShoppingCartSupplierListVo.getCartGoodsList();
                    int totalGoodsQuantity = 0;
                    for (WelfareUserShoppingCartSupplierGoodsVo shoppingCartSupplierGoodsVo : welfareUserShoppingCartSupplierGoodsVoList) {
                        if (EmptyUtils.isNotEmpty(shoppingCartSupplierGoodsVo.getGoodsImg())) {
                            shoppingCartSupplierGoodsVo.setGoodsImg(ImageUtils.setImageSrc(shoppingCartSupplierGoodsVo.getGoodsImg()));
                        }
                        if (EmptyUtils.isNotEmpty(shoppingCartSupplierGoodsVo.getPrice())) {
                            supplierCreditsAmount = supplierCreditsAmount.add(shoppingCartSupplierGoodsVo.getPrice().multiply(BigDecimal.valueOf(shoppingCartSupplierGoodsVo.getQuantity())));
                        }
                        if (EmptyUtils.isNotEmpty(shoppingCartSupplierGoodsVo.getSkuQuantity())){
                            totalGoodsQuantity = totalGoodsQuantity + shoppingCartSupplierGoodsVo.getQuantity();
                        }
                    }
                    welfareUserShoppingCartSupplierListVo.setSupplierCreditsAmount(supplierCreditsAmount);
                    welfareUserShoppingCartSupplierListVo.setSupplierGoodsSize(totalGoodsQuantity);
                    totalCreditsAmount = totalCreditsAmount.add(supplierCreditsAmount);
                }
            }
            welfareUserShoppingCartSupplierVo.setTotalCreditsAmount(totalCreditsAmount);
            welfareUserShoppingCartSupplierVo.setCartGoodsList(welfareUserShoppingCartVoList);
        }
        return welfareUserShoppingCartSupplierVo;
    }

    @Override
    public List<WelfareUserShoppingCartSupplierListVo> getUserCartGoodsListGroupBySupplier(WelfareUserShoppingCartQuery query) {
        PageHelper.startPage(query);
        List<WelfareUserShoppingCartSupplierListVo> welfareUserShoppingCartVoList = welfareUserShoppingCartMapper.getUserCartGoodsListGroupBySupplier(query);
        if (EmptyUtils.isNotEmpty(welfareUserShoppingCartVoList) && welfareUserShoppingCartVoList.size() > 0) {
            dealImg(welfareUserShoppingCartVoList);
        }
        return welfareUserShoppingCartVoList;
    }

    /**
     * 图片处理
     * @param welfareUserShoppingCartVoList 列表对象
     * @return void
     * @author ChenLong
     * @date 2021-06-09 17:19:47
     **/
    private void dealImg(List<WelfareUserShoppingCartSupplierListVo> welfareUserShoppingCartVoList) {
        for (WelfareUserShoppingCartSupplierListVo welfareUserShoppingCartSupplierListVo : welfareUserShoppingCartVoList) {
            if (EmptyUtils.isEmpty(welfareUserShoppingCartSupplierListVo.getCartGoodsList())) {
                List<WelfareUserShoppingCartSupplierGoodsVo> welfareUserShoppingCartSupplierGoodsVoList = welfareUserShoppingCartSupplierListVo.getCartGoodsList();
                welfareUserShoppingCartSupplierGoodsVoList.forEach(shoppingCartSupplierGoodsVo -> {
                    if (EmptyUtils.isNotEmpty(shoppingCartSupplierGoodsVo.getGoodsImg())) {
                        shoppingCartSupplierGoodsVo.setGoodsImg(ImageUtils.setImageSrc(shoppingCartSupplierGoodsVo.getGoodsImg()));
                    }
                });
            }
        }
    }
}
